package com.atguigu.dga230315.governance.assess.calc;

import com.atguigu.dga230315.dolphinscheduler.bean.TDsTaskInstance;
import com.atguigu.dga230315.dolphinscheduler.service.TDsTaskDefinitionService;
import com.atguigu.dga230315.dolphinscheduler.service.TDsTaskInstanceService;
import com.atguigu.dga230315.governance.assess.Assessor;
import com.atguigu.dga230315.governance.bean.AssessParam;
import com.atguigu.dga230315.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga230315.meta.bean.TableMetaInfo;
import com.atguigu.dga230315.meta.constant.MetaConst;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component("TASK_FAILED")
public class TaskFailedAssess extends Assessor {

    @Autowired
    TDsTaskInstanceService tDsTaskInstanceService;

    @Override
    protected void assess(GovernanceAssessDetail assessDetail, AssessParam assessParam) throws ParseException {
        //思路：从DS中的任务运行实例表中 获取 考评日期运行过的任务数据 然后在过滤出 对应表的失败状态的数据

        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();

        //方案一：
/*        List<TDsTaskInstance> tDsTaskInstanceList = tDsTaskInstanceService.list(new QueryWrapper<TDsTaskInstance>()
                .eq("date_format('start_time','%Y-%m-%d')", assessParam.getAssessDate())
                .eq("name", tableMetaInfo.getSchemaName() + "." + tableMetaInfo.getTableName())
                .eq("state", MetaConst.TASK_STATE_FAILED)
        );

        //如果集合中有数据 那么证明考评日期考评的这张表任务运行有报错
        if (tDsTaskInstanceList.size()>0){
            assessDetail.setAssessScore(BigDecimal.ZERO);
            assessDetail.setAssessProblem("任务运行失败");
        }*/

        //方案二：
/*        List<TDsTaskInstance> tDsTaskInstanceList = assessParam.getTDsTaskInstanceList();
        //按照表名和任务状态过滤  将过滤后的数据放到一个新的集合中
        ArrayList<TDsTaskInstance> failedtDsTaskInstances = new ArrayList<>();
        for (TDsTaskInstance tDsTaskInstance : tDsTaskInstanceList) {
            if (tDsTaskInstance.getName().equals(tableMetaInfo.getSchemaName()+"."+tableMetaInfo.getTableName())&&(int)tDsTaskInstance.getState()==MetaConst.TASK_STATE_FAILED){
                failedtDsTaskInstances.add(tDsTaskInstance);
            }
        }
        //如果集合中有数据 那么证明考评日期考评的这张表任务运行有报错
        if (failedtDsTaskInstances.size()>0){
            assessDetail.setAssessScore(BigDecimal.ZERO);
            assessDetail.setAssessProblem("任务运行失败");
        }*/

        //方案三：使用java 流式API编写代码
        List<TDsTaskInstance> tDsTaskInstanceList = assessParam.getTDsTaskInstanceList();

        List<TDsTaskInstance> failedtDsTaskInstances = tDsTaskInstanceList.stream().filter(tDsTaskInstance -> tDsTaskInstance.getName().equals(tableMetaInfo.getSchemaName() + "." + tableMetaInfo.getTableName()) && (int) tDsTaskInstance.getState() == MetaConst.TASK_STATE_FAILED).collect(Collectors.toList());

        //如果集合中有数据 那么证明考评日期考评的这张表任务运行有报错
        if (failedtDsTaskInstances.size()>0){
            assessDetail.setAssessScore(BigDecimal.ZERO);
            assessDetail.setAssessProblem("任务运行失败");
        }
    }
}
